<!--
 * @Description: 
 * @Author: 【大白菜】2601779853@qq.com
 * @Date: 2025-01-21 14:01:06
 * @LastEditTime: 2025-02-19 09:40:04
 * @LastEditors: 【大白菜】2601779853@qq.com
-->
<template>
    <div>
      <a-modal
        :title="isEdit ? '编辑' : '新增'"
        :visible="visible"
        :confirm-loading="confirmLoading"
        :width="600"
        @ok="handleOk"
        @cancel="handleCancel"
      >
        <a-form-model ref="ruleForm" :model="formData" :rules="rules" :label-col="{span: 4}" :wrapper-col="{span: 20}">
          <a-form-model-item v-if="!isEdit" label="上级部门名称">
            <a-input v-model="formData.parent_name" disabled />
          </a-form-model-item>
          <a-form-model-item label="部门名称" prop="dep_name">
            <a-input v-model="formData.dep_name" placeholder="请输入" />
          </a-form-model-item>
        </a-form-model>
      </a-modal>
    </div>
  </template>
    
    <script>
    import { editDepartmentApi } from '@/api/people'
  export default {
    data() {
      return {
        isEdit: false,
        visible: false,
        confirmLoading: false,
        formData: {
          pKey: null,
          dep_name: '', // 部门名称
          parent_key: 0,
          parent_name: ''
        },
        rules: {
          dep_name: [{ required: true, message: '请输入部门名称', trigger: 'change' }]
        },
      }
    },
    methods: {
    add(item) {
        this.isEdit = false
        this.visible = true
        this.formData.pKey = ''
        this.formData.dep_name = ''
        this.formData.parent_name = item.parent_name
        this.formData.parent_key = item.parent_key
      },
      edit(item) {
        this.isEdit = true
        this.visible = true
        this.formData.pKey = item.pKey
        this.formData.dep_name = item.dep_name
        this.formData.parent_name = item.parent_name
        // this.formData.parent_key = item.parent_key
      },
      handleOk() {
        this.$refs.ruleForm.validate(async (valid) => {
          if (valid) {
            this.confirmLoading = true
            let formData = {...this.formData}
            if(!this.isEdit) {
              delete formData.pKey
            }
            delete formData.parent_name
            const {statusCode,data,errors} = await editDepartmentApi(formData)
            this.confirmLoading = false
            if(statusCode == 200) {
              this.visible = false
              this.$parent.getDepartmentThreeList()
            }else {
              this.$message.error(errors)
            }
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      handleCancel() {
        this.visible = false
      },
    },
  }
  </script>
    
    <style lang="scss" scoped>
  </style>